﻿using AutoMapper;
using BaseData.CommonLib.Models;
using BaseData.DomainLib.AggregatesModel.SaleOrderAggregate;
using MediatR;
using Swimj.CommonLib.Base;

namespace BaseData.ApplicationLib.Commands;

public class UpdateSaleOrderCommand : SaleOrderEditRequestDto, IRequest<Result<SaleOrderInfoResponseDto>>
{
    public int Id { get; set; }
}

public class UpdateSaleOrderCommandHandler : IRequestHandler<UpdateSaleOrderCommand, Result<SaleOrderInfoResponseDto>>
{
    private readonly ISaleOrderRepository _saleOrderRepository;
    private readonly IMapper _mapper;

    public UpdateSaleOrderCommandHandler(ISaleOrderRepository saleOrderRepository,
        IMapper mapper)
    {
        this._saleOrderRepository = saleOrderRepository;
        this._mapper = mapper;
    }

    public async Task<Result<SaleOrderInfoResponseDto>> Handle(UpdateSaleOrderCommand request,
        CancellationToken cancellationToken)
    {
        var saleOrder = await _saleOrderRepository.Get(request.Id);
        // saleOrder?.Update();

        await _saleOrderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

        return await Result<SaleOrderInfoResponseDto>.SuccessAsync(_mapper.Map<SaleOrderInfoResponseDto>(saleOrder));
    }
}